{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [],
   "source": [
    "\n",
    "import copy\n",
    "class Solution:\n",
    "    def minPathSum(self, grid) -> int:\n",
    "        rows = len(grid)\n",
    "        cols = len(grid[0])\n",
    "        path = [[0]*cols for x in range(rows)]\n",
    "        path[0][0] = grid[0][0]\n",
    "        for i in range(1,cols):\n",
    "            path[0][i] = path[0][i-1] + grid[0][i]\n",
    "        for i in range(1, rows):\n",
    "            path[i][0] = path[i-1][0] + grid[i][0]\n",
    "        for r in range(1, rows):\n",
    "            for c in range(1, cols):\n",
    "                if(path[r][c-1] < path[r-1][c]):\n",
    "                    path[r][c] = grid[r][c] + path[r][c-1]\n",
    "                else:\n",
    "                    path[r][c] = grid[r][c] + path[r-1][c]\n",
    "        return path[rows-1][cols-1]\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "7\n"
     ]
    }
   ],
   "source": [
    "grid = [\n",
    "  [1,3,1],\n",
    "  [1,5,1],\n",
    "  [4,2,1]\n",
    "]\n",
    "a = Solution()\n",
    "print(a.minPathSum(grid))"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.4"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
